package com.csmht.tododemo.service.impl;

import com.csmht.tododemo.mapper.CommentMapper;
import com.csmht.tododemo.pojo.dto.CommentDTO;
import com.csmht.tododemo.pojo.dto.CommentPageDTO;
import com.csmht.tododemo.pojo.entity.Comment;
import com.csmht.tododemo.pojo.vo.CommentVO;
import com.csmht.tododemo.result.Result;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;

import static com.csmht.tododemo.constant.MessageConstant.DATA_ERR;

@Service
public class CommentServiceImpl implements com.csmht.tododemo.service.CommentService {

    @Resource
    private CommentMapper commentMapper;

    @Override
    public CommentVO add(CommentDTO commentDTO) {

        Comment comment = new Comment();



        BeanUtils.copyProperties(commentDTO, comment);

        commentMapper.insert(comment);

        CommentVO commentVO = new CommentVO();

        BeanUtils.copyProperties(comment, commentVO);

        return commentVO;
    }

    @Override
    public List<CommentVO> mainList(CommentPageDTO commentPageDTO) {

        List<Comment> list = commentMapper.selectMainList(commentPageDTO);

        List<CommentVO> ans = new ArrayList<>();
        list.forEach(item -> {
            CommentVO comment = new CommentVO();
            BeanUtils.copyProperties(item, comment);
            ans.add(comment);
        });

        return ans;
    }

    @Override
    public List<CommentVO> List(Long id, CommentPageDTO commentPageDTO) {

        List<Comment> list = commentMapper.selectSmallList(id,commentPageDTO);

        List<CommentVO> ans = new ArrayList<>();
        list.forEach(item -> {
            CommentVO comment = new CommentVO();
            BeanUtils.copyProperties(item, comment);
            ans.add(comment);
        });

        return ans;
    }
}
